package com.vogue.supplymall.order.domain.biz.impl;

import com.vogue.supplymall.common.domain.dto.RefundMaxCountInfo;
import com.vogue.supplymall.common.exception.DataNotFindException;
import com.vogue.supplymall.order.domain.biz.RefundOrderDetailBiz;
import com.vogue.supplymall.order.domain.service.RefundDetailService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * The type Refund order detail biz.
 */
@Service("refundOrderDetailBiz")
public class RefundOrderDetailBizImpl implements RefundOrderDetailBiz {

    /**
     * The constant LOGGER_REFUNDORDERDETAILBIZIMPL.
     */
    public static final Logger LOGGER_REFUNDORDERDETAILBIZIMPL = LoggerFactory.getLogger(OrderDetailBizImpl.class);

    @Resource
    private RefundDetailService refundDetailService;

    /*获取订单买家*/
    /**
     * 第一个Map<String, Object>，传入的参数类型，第二个是得到的结果类型
     * getOrderBuyer 方法名
     * firstData 传入的参数
     * entrySet() 将传入的结果转化为一个set集合
     * stream()遍历set集合
     * filter 遍历过程中进行过滤
     * entry.getKey().startsWith("ob") 过滤条件
     * collect 收集结果
     */
    private Function<Map<String, Object>, Map<String, Object>> getOrderBuyer = (firstData) -> firstData
            .entrySet()
            .stream()
            .filter((entry) -> entry.getKey().startsWith("ob"))
            .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

    /*获取订单卖家*/
    private Function<Map<String, Object>, Map<String, Object>> getOrderSeller = (firstData) -> firstData
            .entrySet()
            .stream()
            .filter((entry) -> entry.getKey().startsWith("os"))
            .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    /*goodsData内容过滤*/
    private Function<List<Map<String, Object>>, List<Map<String, Object>>> fiterBuySell = (list) -> list
            .stream()
            .map((Map<String, Object> map) -> map.entrySet()
                    .parallelStream()
                    .filter((entry) -> !entry.getKey().startsWith("os") || !entry.getKey().startsWith("ob") || !entry.getKey().startsWith("oc")
                            || !entry.getKey().startsWith("ffm") || !entry.getKey().startsWith("ru"))
                    .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)))
            .collect(Collectors.toList());

    /*物流信息内容过滤*/
    private Function<Map<String, Object>, Map<String, Object>> fulfillment = (firstData) -> firstData
            .entrySet()
            .stream()
            .filter((entry) -> entry.getKey().startsWith("ffm"))
            .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

    /*图片信息内容过滤*/
//    private Function<List<Map<String, Object>>, List<Map<String, Object>>> refundImgs = (list) -> list
//            .stream()
//            .map((Map<String, Object> map) -> map.entrySet()
//                    .parallelStream()
//                    .filter((entry) -> entry.getKey().startsWith("ru"))
//                    .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)))
//			.filter(map -> StringUtils.isNotEmpty(map.get("ru_url").toString()))
//            .collect(Collectors.toList());

    @Override
    public Map<String, Object> findRefundOrderDetail(Long refundOrderId) {
        List<Map<String, Object>> orderDetail = refundDetailService.findRefundOrderDetail(refundOrderId);
        List<Map<String, Object>> orderRefundUpload = refundDetailService.findRefundOrderRefundUpload(refundOrderId);
        if (null == orderDetail) {
            throw new DataNotFindException("");
        }
        Map<String, Object> firstData = orderDetail.stream().findFirst().get();
        Map<String, Object> result = new HashMap<>();
        result.put("buyer", getOrderBuyer.apply(firstData));
        result.put("seller", getOrderSeller.apply(firstData));
        result.put("goodsData", fiterBuySell.apply(orderDetail));
//	        result.put("refundImgs", fiterBuySell.apply(orderDetail));
        result.put("fulfillment", StringUtils.isNotEmpty((String) fulfillment.apply(firstData).get("ffm_number"))
                ? fulfillment.apply(firstData) : new HashMap<>());
        result.put("refundImgs", orderRefundUpload);
        result.put("totalprice", firstData.get("totalprice"));
        result.put("totalquantity", firstData.get("totalquantity"));
        result.put("freight", firstData.get("freight"));
        result.put("orderstatus", firstData.get("orderstatus"));
        // 2018/2/26 add供应链侧是否拉取过订单
        result.put("pullstate", firstData.get("pullstate"));
        result.put("memo", firstData.get("memo"));
        result.put("ordernumber", firstData.get("ordernumber"));
        result.put("finalprice", firstData.get("finalprice"));
        result.put("isInvoice", firstData.get("isInvoice"));
        result.put("cause", firstData.get("cause"));
        //2018/8/3 add 增加退款方式
        result.put("refundway", firstData.get("refundway"));

        result.put("createdate", firstData.get("createdate"));

        return result;
    }

    @Override
    public Map<String, Object> getRefundMaxQuantity(RefundMaxCountInfo refundMaxCountInfo) {
        Map<String, Object> result = new HashMap<>();

        BigDecimal itemquantity = new BigDecimal(refundMaxCountInfo.getItemquantity());

        List<Map<String, Object>> mqList = refundDetailService.getRefundMaxQuantity(refundMaxCountInfo);
        //有退货数量
        if (mqList.size() > 0) {
            BigDecimal refundMaxQuantity = (BigDecimal) mqList.get(0).get("refundquantity");
            result.put("refundmaxquantity", itemquantity.subtract(refundMaxQuantity));
            result.put("orderid", refundMaxCountInfo.getOrderid());
            result.put("oipid", refundMaxCountInfo.getOipid());
        } else {
            result.put("refundmaxquantity", itemquantity);
            result.put("orderid", refundMaxCountInfo.getOrderid());
            result.put("oipid", refundMaxCountInfo.getOipid());
        }

        return result;
    }


}
